<!-- 收文办理列表 -->
<template>
  <view>
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
      <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="load_more"
                offset="20" :error.sync="error" error-text="请求失败，点击重新加载"
                :immediate-check="immediateCheck">
        <template v-if="receType == 'mhyw' || receType == 'mhxw'">
          <van-cell v-for="item in list" :title="item.infoTitle" :value="dateFormat(item.publishDate)"
                    @click="openDetail(item, 'news')" value-class="text-sm"/>
        </template>
        <template v-else-if="receType == 'mhjwd' || receType == 'zhswd'
                      || receType == 'gljwd' || receType == 'jgjwd'">
          <van-cell v-for="item in list" :title="item.wdTitle" :value="item.publishDate"
                    @click="toWdDetail(item.wdId)" value-class="text-sm"/>
        </template>
        <template v-else-if="receType == 'zywj'">
          <van-cell v-for="item in list" :title="item.title" :value="dateFormat(item.pushTime)"
                    @click="toFileDetail(item.id)" value-class="text-sm"/>
        </template>
        <template v-else-if="receType == 'hlwzy'">
          <van-cell v-for="item in list" :title="item.title" :value="dateFormat(item.pushTime)"
                    @click="openHlwzy(item.id)" value-class="text-sm"/>
        </template>
      </van-list>
    </van-pull-refresh>
  </view>
</template>

<script>

export default {
  props: {
    newsType: {
      type: String
    },
    finishStatus: {
      type: Boolean,
      default: false
    },
    title: {//搜索内容
      type: String,
      default: ''
    },
  },
  data() {
    return {
      list: [],
      loading: false,   // 加载状态
      finished: false,  // 是否加载
      refreshing: false,// 是否刷新
      error: false,     // 错误状态
      pageNum: 1,   		// 页数
      pageSize: 10, 		// 条数
      receType: '', 		// 类型
      immediateCheck: true, // 是否在初始化时立即执行滚动位置检查　　　
    }
  },

  watch: {
    newsType: {
      handler(nval, oval) {
        this.receType = nval
        this.onRefresh()
      },
      immediate: true,
      deep: true
    },
    finishStatus: {
      handler(nval, oval) {
        this.finished = nval
      },
      immediate: true,
      deep: true
    },
    //查询的标题名称
    title:{
      handler(nVal, oVal) {
        this.searchContent = nVal
        this.onRefresh()
      },
      immediate: true,
      deep: true
    }
  },

  methods: {
    // 下拉刷新
    onRefresh() {
      // 先清空数据
      this.list = []
      // 将没有更多的状态改成false
      this.finished = false
      // 默认加载第一页
      this.pageNum = 1
      this.getList()
      this.refreshing = false
    },

    // 上拉加载
    load_more() {
      // 页数设置
      if (this.list.length == 0) {
        this.pageNum = 1
      } else {
        this.pageNum++
      }
      // 将下拉刷新停止
      this.refreshing = false;
      setTimeout(() => {
        this.getList()
      }, 500)
    },

    // 获取收文数据
    getList() {
      let url = ''
      let paramData = {
        'systemKey': this.cache.get('userInfo').systemKey,
        'systemFlag': this.cache.get('userInfo').systemFlag,
        'page': this.pageNum,
        'rows': this.pageSize,
      }
      if (this.receType == 'zywj') {//重要文件
        url = '/caac/oa/app/getImportantDocuments'
        this.$set(paramData, "title",this.searchContent)
      }
      if (this.receType == 'mhjwd') {//民航局文电
        url = '/caac/oa/doc/wdcx/mhj/getMhjWdList'
        this.$set(paramData, "wdTitle",this.searchContent)
      }
      if (this.receType == 'zhswd') {//综合司文电
        url = '/caac/oa/doc/wdcx/mhj/getSjWdManageList'
        this.$set(paramData, "wdTitle",this.searchContent)
      }
      if (this.receType == 'mhyw') {//局领导活动动态
        url = '/caac/oa/mhNewsType/queryMhNewsYwlist'
        this.$set(paramData, "infoTitle",this.searchContent)
      }
      if (this.receType == 'mhxw') { //行业动态
        url = '/caac/oa/mhNewsType/getMhinfoIndustryNews'
        this.$set(paramData, "infoTitle",this.searchContent)
      }
      if (this.receType == 'hlwzy') { //互联网摘要
        url = '/caac/oa/app/getInternetSummary'
        this.$set(paramData, "title",this.searchContent)
      }
      if (this.receType == 'gljwd') { //管理局文电
        url = '/caac/oa/doc/wdcx/mhj/getOfficeDeptWdList'
        this.$set(paramData, "wdTitle",this.searchContent)
      }
      if (this.receType == 'jgjwd') {
        url = '/caac/oa/doc/wdcx/mhj/getJgJDeptWdList'
        this.$set(paramData, "wdTitle",this.searchContent)
      }
      // 加载状态
      this.loading = true
      if (this.receType == 'zywj' || this.receType == 'hlwzy') {
        this.$http({
          url: this.$http.adornUrl(url),
          method: 'get',
          params: this.$http.adornParams(paramData)
        }).then(({data}) => {
          if (data.code == 200) {
            if (data.data != null && JSON.stringify(data.data) != '{}') {
              if (this.pageNum == 1) {
                this.list = data.data.data.rows
              } else {
                data.data.data.rows.forEach(t => this.list.push(t))
              }
              // 判断当前页是否为最后一页
              if ((this.pageNum * this.pageSize) > data.data.data.total) {
                this.finished = true
              }
            } else {
              this.finished = true
            }
          } else {
            this.error = true
          }
        }).catch((err) => {
          this.error = true
        }).finally(() => {
          // 关闭加载状态
          this.loading = false
        })
      } else {
        this.$http({
          url: this.$http.adornUrl(url),
          method: 'get',
          params: this.$http.adornParams(paramData)
        }).then(({data}) => {
          if (data.code == 200) {
            if (data.data != null && JSON.stringify(data.data) != '{}') {
              if (this.pageNum == 1) {
                this.list = data.data.rows
              } else {
                data.data.rows.forEach(t => this.list.push(t))
              }
              // 判断当前页是否为最后一页
              if ((this.pageNum * this.pageSize) > data.data.total) {
                this.finished = true
              }
            } else {
              this.finished = true
            }
          } else {
            this.error = true
          }
        }).catch((err) => {
          this.error = true
        }).finally(() => {
          // 关闭加载状态
          this.loading = false
        })
      }
    },

    // 时间格式化
    dateFormat(dateTime) {
      let formatStr = dateTime
      if (dateTime != null && dateTime != '' && dateTime.length > 10) {
        formatStr = dateTime.substr(0, 10)
      }
      return formatStr
    },

    // 打开详情
    openDetail(item, type) {
      uni.navigateTo({
        url: '/pages/admin/components/newsList/compo-detail?from=' + type,
        success: function (res) {
          // 通过eventChannel向被打开页面传送数据
          res.eventChannel.emit('acceptDataFromOpenerPage', item)
        }
      })
    },

    // 打开文电
    toWdDetail(wdId) {
      this.$http({
        url: this.$http.adornUrl(`/caac/oa/doc/wdcx/glj/getFileList`),
        method: 'get',
        params: this.$http.adornParams({
          'systemKey': this.cache.get('userInfo').systemKey,
          'systemFlag': this.cache.get('userInfo').systemFlag,
          'wdId': wdId,
        })
      }).then(({data}) => {
        console.log('toFileDetail:' + JSON.stringify(data))
        if (data.code == 200) {
          if (this.util.isNotEmpty(data.data[0])) {
            let attachId = data.data[0].attachId
            let attachName = data.data[0].attachName
            let attachType = data.data[0].attachType
            if (attachId) {
              attachType = attachType.toLowerCase()
              attachName = this.util.trim(attachName.split('.')[0]) + '.' + attachName.split('.')[1].toLowerCase()
              let url = '/caac/platform/UploadChunkController/download?attachId=' + attachId +
                      '&systemKey=' + this.cache.get('userInfo').systemKey +
                      '&systemFlag=' + this.cache.get('userInfo').systemFlag
              let openUrl = this.$http.adornUrl(url)
              // 打开文电
              this.lx.openFileDetail(attachName, attachType, openUrl)
            }
          } else {
            this.$toast('没有文件可供查看')
          }
        } else {
          this.$toast(data.message)
        }
      })
    },


    // 跳转文件详情
    toFileDetail(id) {
      this.$http({
        url: this.$http.adornUrl(`/caac/oa/app/getImportantDocumentsDetails`),
        method: 'get',
        params: this.$http.adornParams({
          'systemKey': this.cache.get('userInfo').systemKey,
          'systemFlag': this.cache.get('userInfo').systemFlag,
          'id': id,
        })
      }).then(({data}) => {
        if (data.code == 200) {
          let annexs = data.data.data.annexs
          if (annexs == null || annexs.length == 0) {
            uni.navigateTo({
              url: '/pages/caac/components/newsList/compo-detail?from=zywj',
              success: function (res) {
                // 通过eventChannel向被打开页面传送数据
                res.eventChannel.emit('acceptDataFromOpenerPage', data.data.data)
              }
            })
          }else{
            let attachId = annexs[0].attachId
            if (attachId) {
              // 获取文件后缀
              this.getFileExtension(attachId)
            }
          }
        } else {
          this.$toast(data.message)
        }
      })
    },
    // 获取文件后缀
    getFileExtension(attachId) {
      this.$http({
        url: this.$http.adornUrl(`/caac/oa/app/getFileByIds`),
        method: 'get',
        params: this.$http.adornParams({
          'systemKey': this.cache.get('userInfo').systemKey,
          'systemFlag': this.cache.get('userInfo').systemFlag,
          'attachId': attachId,
        })
      }).then(({data}) => {
        if (data.code == 200) {
          if (this.util.isNotEmpty(data.data.data[0])) {
            let attachType = data.data.data[0].attachType
            let attachName = data.data.data[0].attachName

            attachType = attachType.toLowerCase()
            attachName = this.util.trim(attachName.split('.')[0]) + '.' + attachName.split('.')[1].toLowerCase()
            let url = '/caac/oa/app/download?attachId=' + attachId + `&attachName=` + attachName +
                    '&systemKey=' + this.cache.get('userInfo').systemKey +
                    '&systemFlag=' + this.cache.get('userInfo').systemFlag
            let openUrl = this.$http.adornUrl(url)
            // 打开文件详情
            this.lx.openFileDetail(attachName, attachType, openUrl)
          } else {
            this.$toast('没有文件可供查看')
          }
        } else {
          this.$toast(data.message)
        }
      })
    },

    // 互联网摘要详情
    openHlwzy(id) {
      this.$http({
        url: this.$http.adornUrl(`/caac/oa/app/getInternetSummaryDetails`),
        method: 'get',
        params: this.$http.adornParams({
          'id': id,
          'systemKey': this.cache.get('userInfo').systemKey,
          'systemFlag': this.cache.get('userInfo').systemFlag,
        })
      }).then(({data}) => {
        if (data.code == 200) {
          let annexs = data.data.data.annexs
          // 新闻类型的跳转详情
          if (annexs == null || annexs.length == 0) {
            uni.navigateTo({
              url: '/pages/admin/components/newsList/compo-detail?from=hlwzy',
              success: function (res) {
                // 通过eventChannel向被打开页面传送数据
                res.eventChannel.emit('acceptDataFromOpenerPage', data.data.data)
              }
            })
          } else {
            // 文件类型的用其他软件打开
            let attachId = annexs[0].attachId
            let attachName = annexs[0].annexName
            let attachType = attachName.split('.')[1]
            let openUrl = '/caac/platform/UploadChunkController/download?attachId=' + attachId +
                    '&systemKey=' + this.cache.get('userInfo').systemKey +
                    '&systemFlag=' + this.cache.get('userInfo').systemFlag
            this.lx.openFileDetail(attachName, attachType.toLowerCase(), openUrl)
          }
        }
      })
    },

  },


}
</script>

<style>
	.van-cell__title {
		min-width: 75%;

		span {
			display: inline-block;
			text-align: left;
			word-break: break-all;
		}
	}
</style>
